<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class App extends CI_Controller {
	public function __construct() {
		parent::__construct ();
	}
	 /*
    * 管理员APP登录
    */
    public function login() {
		$username = $this->input->post ( 'username' );
		$password = $this->input->post ( 'password' );
		$this->load->model ( 'admin_model' );
		$this->load->model ( 'category_model' );
		if ($admin = $this->admin_model->get_admin ( $username, md5 ( $password . 'jishibao' ) )) {
			$token=$admin['token'];
			if(empty($token)){
				$token=md5 ( $username.$password . 'jishibao' );
				$newadmin = array (
					'token' => $token
				);
				$this->admin_model->update($admin['id'],$newadmin);
			}
            $data['token'] = $token;
			$category=$this->category_model->load ( $admin['categoryid'] );
			$data ['category'] = $category['name'];
			return_json ( TRUE,$data, '登陆成功！' );
		} else {
			return_json ( FALSE, '', '用户名或密码错误！' );
		}
	}
	/*
	 * 刷卡取餐
	 */
	public function takeOrder(){
		$token = $this->input->post ( 'token' );
        $this->load->model ( 'admin_model' );
        if(empty($token)){
            return_json(FALSE,'','token为空！');
        }
        $admin = $this->admin_model->find_by_token ($token);
        if(empty($admin)){
            return_json(FALSE,'','token不存在！');
        }
		$no = $this->input->post ( 'no' );
		$this->load->model ( 'user_model' );
		$this->load->model ( 'order_model' );
		$this->load->model ( 'orderdetail_model' );
		$this->load->model ( 'config_model' );
		$this->load->model ( 'pickup_model' );
		$user=$this->user_model->find_by_no ( $no );
		if(empty($user)){
			return_json(FALSE,'','卡号不存在！');
		}
		$date=date('Y-m-d');
		$now=date ( "H:i" );
		$config1 = $this->config_model->find_by_key ("bsdate");
		$config2 = $this->config_model->find_by_key ("bedate");
		$config3 = $this->config_model->find_by_key ("lsdate");
		$config4 = $this->config_model->find_by_key ("ledate");
		$config5 = $this->config_model->find_by_key ("dsdate");
		$config6 = $this->config_model->find_by_key ("dedate");
		if(strtotime($now)>=strtotime($config1['value'])&&strtotime($now)<strtotime($config2['value'])){
			$type=1;//早餐
		}elseif(strtotime($now)>=strtotime($config3['value'])&&strtotime($now)<strtotime($config4['value'])){
			$type=2;//中餐
		}elseif(strtotime($now)>=strtotime($config5['value'])&&strtotime($now)<=strtotime($config6['value'])){
			$type=3;//晚餐
		}else{
			return_json(FALSE,'','非取餐时间！');
		}
		$temp = $this->pickup_model->count_by_user_type_date($user['id'],$type,$date);
		if(!empty($temp)){
			return_json(FALSE,'','已取餐！');
		}
		$order = $this->order_model->find_by_user_date($user['id'],$date,$type);
		$data['name']=$user['sname'];
		$data['no']=$user['sno'];
		if(empty($order)){
			if($admin['categoryid']!=4){
				return_json(FALSE,'',$user['sname'].' 请到默认餐窗口取餐！');
			}
			$name="默认套餐";
		}else{
			$name=$order['name'];
			if($order['categoryid']!=$admin['categoryid']){
				return_json(FALSE,$order['categoryid'],$user['sname'].' 请到'.$name.'窗口取餐！');
			}
		}
		$pickup = array (
			'type' => $type,
			'userid' => $user['id'],
			'pickup_time' =>  date ( "Y-m-d H:i:s" ) 
		);
		$this->pickup_model->create ($pickup);
		/*
		if($order['take']==1){
			return_json(FALSE,'','已取餐！');
		}*/
		/*
		$detail = array (
			'take' => 1
		);
		$this->orderdetail_model->update ( $order['id'],$detail );*/
		return_json(TRUE,$user['sname'],$name.'取餐成功！');
	}
}